home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 247_02 / ratcalc.c < prev    next >
Text File  |  1989-04-17  |  21KB  |  786 lines

  1. /*
  2.  *       MIRACL RATIONAL CALCULATOR - IBM-PC VERSION
  3.  *       Compiles with Turbo C V1.0 or Microsoft C V3.0+
  4.  *  **   Change display mode for 40/80 column operation (e.g. 'mode 40' to DOS)
  5.  *  **   Change colour/B&W below
  6.  */
  7.  
  8. #include <stdio.h>     /*** IBM-PC specific section ***/
  9. #include <dos.h>        
  10. #include "miracl.h"
  11.  
  12. #define ESC 27
  13. #define SPACE 32
  14. #define BELL 7
  15.  
  16. #define DVERT 186
  17. #define DHORZ 205
  18. #define DTLHC 201
  19. #define DTRHC 187
  20. #define DBLHC 200
  21. #define DBRHC 188
  22. #define VERT 179        
  23. #define HORZ 196
  24. #define TLHC 218
  25. #define TRHC 191
  26. #define BLHC 192
  27. #define BRHC 217
  28. #define LSIDE 199
  29. #define RSIDE 182
  30.  
  31. /* Globals */
  32. /* set colours          B/W       Colours (suggested) */
  33.  
  34. int ORDINARY=0x07; /*   0x07        0x17    blue-white        */
  35. int INVER   =0x70; /*   0x70        0x40    red-black         */
  36. int BOLD    =0x0F; /*   0x0F        0x70    white-black       */
  37. int BLINKING=0x87; /*   0x87        0xF4    white-red (blink) */
  38. int HELPCOL =0x07; /*   0x07        0x02    black-green       */
  39. int PRESSED =0x0F; /*   0x0F        0x4F    red-white (bold)  */
  40. int STATCOL =0x07; /*   0x07        0x74    white-red         */
  41. int BGROUND =0x07; /*   0x07        0x07    black-white       */
  42.  
  43. int dmode;
  44.  
  45.                 /*** Constants 16-bit values ***/
  46.  
  47. char cpi[] = "2646693125139304345/842468587426513207";
  48. char clg2[]= "28575556407596679551/41225813519882571900";
  49. char clg10[]="9948944967727459672/4320771900243161573";
  50.  
  51.                 /*** Device independent data ***/
  52.  
  53. extern big w15;
  54.  
  55. char *settings[4][4]= {"   ","HYP","","",
  56.                        "Be ","B2 ","B10","",
  57.                        "RAD","DEG","GRA","",
  58.                        "DEC","HEX","OCT","BIN"};
  59. int nops[]= {1,2,1,3};         /* number of options   */
  60. int opp[] = {0,1,1,2,2,3,3,2}; /* operator precedence */
  61.  
  62. char *keys[6][7]= {"SIN","COS","TAN","EXP","F-D","CLR","OFF",
  63.                    "ASN","ACS","ATN","LOG","SET","MOD","DEL",
  64.                    " 7 "," 8 "," 9 ","Y^X","X√Y"," ÷ "," RM",    /******/
  65.                    " 4 "," 5 "," 6 "," X²","²√X"," x "," M+",
  66.                    " 1 "," 2 "," 3 ","1/X"," π "," - ","+/-",
  67.                    " 0 "," / "," . "," ( "," ) "," + "," = "};
  68.  
  69. int qkeys[6][7]=  {'s','k','t','e','f','c','o',
  70.                    'S','K','T','l','g','%','<',
  71.                    '7','8','9','y','x','\\','r',
  72.                    '4','5','6','q','v','*','m',
  73.                    '1','2','3','i','p','-',';',
  74.                    '0','/','.','(',')','+','='};
  75.  
  76. char *htext[]=  {"Arrow keys find, space bar activates OR",
  77.                  "use numeric keys  (with  A-F  for hex),",
  78.                  "brackets,  and mnemonic keys as below: ",
  79.                  " FUNCTION      KEY   FUNCTION       KEY",
  80.                  "   SIN          s      ASN           S ",
  81.                  "   COS          k      ACS           K ",
  82.                  "   TAN          t      ATN           T ",
  83.                  "   EXP(e/2/10)  e      LOG(e/2/10)   l ",
  84.                  "   Y^X          y      X√Y           x ",   /******/
  85.                  "    X²          q      ²√X           v ",
  86.                  "   1/X          i       π            p ",
  87.                  "   F-D          f      SET           g ",
  88.                  "   MOD          %      CLR           c ",
  89.                  "    ÷      d or \\       x       b or * ",
  90.                  "    -           -       +       , or + ",
  91.                  "   OFF          o      DEL    DEL or < ",
  92.                  "    RM          r       M+           m ",
  93.                  "   +/-          ;       =     RET or = ",
  94.                  "Note: / means 'over'  as in fractions  ",
  95.                  "F-D converts  fraction  <-->  decimal  ",
  96.                  "SET changes settings - use arrow keys  ",
  97.                  "Alternative arrow keys -   u  h  j  n  ",
  98.                  "Report any problems to M.Scott |NIHED  ",
  99.                  "This program is Public Domain  |Dublin "};     /******/
  100.  
  101. char display[]=   "   MIRACL RATIONAL CALCULATOR V2.0   ";
  102. char  status[]=   "                                     ";
  103. char oldstat[]=   "                                     ";
  104.  
  105. int erptr=0;
  106. int mmptr=6;
  107. int exptr=10;
  108. int typtr=16;
  109. int stptr[]={22,26,30,34};
  110.  
  111. int dbeg=2;
  112. int dlen=37;
  113. int top=7;
  114. int width=80;      /* screen width can be 40 or 80 columns */
  115. char mybuff[40];
  116.  
  117. flash x,y[8],m,t;
  118. flash radeg,loge2,loge10;
  119. int ipt,op[8],prop[8],sp,brkt,lgbase;
  120. bool flag,newx,result,hyp,degrees,delim;
  121. int option[]={0,0,0,0};
  122.  
  123. /* Device specific code - IBM-PC versions */
  124.  
  125. void curser(x,y)
  126. int x,y;
  127. { /* position cursor at x,y */
  128.     union REGS regs;
  129.     regs.h.ah=2;
  130.     regs.h.dh=y-1;
  131.     regs.h.dl=x-1;
  132.     regs.h.bh=0;              /* Video page 0 */
  133.     int86(0x10,®s,®s);  /* Use Dos Interrupt 10h */
  134. }
  135.  
  136. void screen()
  137. { /* initialise screen */
  138.     union REGS regs;
  139.     regs.h.ah=0x0F;  /* get screen mode */
  140.     regs.h.al=0;
  141.     int86(0x10,®s,®s);
  142.     dmode=regs.h.al;
  143.     if ((dmode&2)!=0) width=80;
  144.     else                  width=40;
  145.     regs.h.ah=6;     /* clear screen */
  146.     regs.h.al=0;
  147.     regs.h.cl=0;
  148.     regs.h.ch=0;
  149.     regs.h.dl=width-1;
  150.     regs.h.dh=24;
  151.     regs.h.bh=BGROUND;
  152.     regs.h.bl=0;
  153.     int86(0x10,®s,®s);
  154. }
  155.  
  156. void restore()
  157. {  /* restore situation */
  158.     union REGS inregs,outregs;
  159.     inregs.h.ah=0;       /* reset initial display mode */
  160.     inregs.h.al=dmode;
  161.     int86(0x10,&inregs,&outregs);
  162. }
  163.  
  164. void apchar(attr,x,y,ch)
  165. int attr,x,y;
  166. char ch;
  167. { /* output character with attribute at x,y */
  168.     union REGS regs;
  169.     curser(x,y);
  170.     regs.h.ah=9;              /* output a character */
  171.     regs.h.al=ch;
  172.     regs.h.bh=0;
  173.     regs.h.bl=attr;
  174.     regs.h.cl=1;
  175.     regs.h.ch=0;
  176.     int86(0x10,®s,®s);
  177. }
  178.  
  179. void aprint(attr,x,y,text)
  180. int attr,x,y;
  181. char* text;
  182. { /* attribute print */
  183.     while (*text!='\0')
  184.     {
  185.         apchar(attr,x++,y,*text);
  186.         text++;
  187.     }
  188. }
  189.  
  190. void lclr(x,y)
  191. int x,y;
  192. { /* clear from x,y to end of line */
  193.     aprint(BGROUND,x,y,"                                       ");
  194. }
  195.  
  196. void cset(k)
  197. int k;
  198. { /* select special character set */
  199.     return;
  200. }
  201.  
  202. int gethit()
  203. { /* get single keystroke */
  204.     int ch;
  205.     ch=getch();
  206.     if (ch!=0) return ch;
  207.     ch=getch();
  208.     if (ch==72) return 'u';  /* transform some useful extended codes */
  209.     if (ch==75) return 'h';
  210.     if (ch==77) return 'j';
  211.     if (ch==80) return 'n';
  212.     if (ch==83) return 127;
  213.     return 0;
  214. }
  215.  
  216. /*** Device independent code ***/
  217.  
  218. int arrow(c)
  219. int c;
  220. { /* check for arrow key             *
  221.    * returns 1 for up, 2 for down,   *
  222.    * 3 for right, 4 for left, else 0 */
  223.     if (c=='u') return 1;
  224.     if (c=='n') return 2;
  225.     if (c=='j') return 3;
  226.     if (c=='h') return 4;
  227.     return 0;
  228. }
  229.  
  230. void instat(ptr,strg)
  231. int ptr;
  232. char *strg;
  233. { /* insert a status setting into status line */
  234.     strncpy(&status[ptr],strg,strlen(strg));
  235. }
  236.  
  237. void getstat()
  238. { /* set status line */
  239.     int i;
  240.     if (ERNUM) instat(erptr,"ERROR");
  241.     else       instat(erptr,"     ");
  242.     if (EXACT) instat(exptr,"EXACT");
  243.     else       instat(exptr,"     ");
  244.     if (size(m)!=0) instat(mmptr,"MEM");
  245.     else            instat(mmptr,"   ");
  246.     if (POINT) instat(typtr,"POINT");
  247.     else       instat(typtr,"FRACT");
  248.     for (i=0;i<4;i++)
  249.          instat(stptr[i],settings[i][option[i]]);
  250. }
  251.  
  252. void setopts()
  253. { /* set options */
  254.     if (option[0]==0) hyp=FALSE;
  255.     else              hyp=TRUE;
  256.     lgbase=0;
  257.     if (option[1]==1) lgbase=2;
  258.     if (option[1]==2) lgbase=10;
  259.     if (option[2]==0) degrees=FALSE;
  260.     else              degrees=TRUE;
  261.     IOBASE=10;
  262.     if (option[3]==1) IOBASE=16;
  263.     if (option[3]==2) IOBASE=8;
  264.     if (option[3]==3) IOBASE=2;
  265.  
  266. void show(force)
  267. bool force;
  268. { /* output display */
  269.     if (force || strcmp(oldstat,status)!=0)
  270.     {
  271.         if (ERNUM) aprint(BLINKING,2,2,status);
  272.         else       aprint(STATCOL,2,2,status);
  273.         strcpy(oldstat,status);
  274.     }
  275.     aprint(BOLD,dbeg,3,display);
  276. }